<?php
$LIB = dirname(__FILE__);

require_once($LIB. "/vendor/autoload.php");
use League\HTMLToMarkdown\HtmlConverter;

// const
$host = "https://api.juejin.cn/";
$api_get_booklet = "booklet_api/v1/booklet/get";
$api_get_section = "booklet_api/v1/section/get";
$origin = "https://juejin.cn";

// config
$config = getConfig();
$aid = $config['aid'];
$uuid = $config['uuid'];
$booklet_id = $config['booklet_id'];
$cookie = $config['cookie'];

// output
$md_dir = "dist/md";
$html_dir = "dist/html";

info("output md dir:". $md_dir);
info("output html dir:". $html_dir);

@mkdir("dist", 0755, true);
@mkdir($md_dir, 0755, true);
@mkdir($html_dir, 0755, true);

// 有些小册没有md，可以使用该工具转换
$converter = new HtmlConverter();
$converter->getConfig()->setOption('strip_tags', true);

// 获取章节目录
$sections = getBooklet();
// print_r($booklet);

$i = 1;
foreach ($sections as $section) {
    $content = getContent($section['section_id']);
    // print_r($content);
    $content_show = $content['content'];
    $markdown_show = $content['markdown_show'];
    // if (empty($markdown_show) && !empty($content_show)) {
    if (!empty($content_show)) {
        $markdown_show = $converter->convert($content_show);
        // 去除pre里的标签
        $markdown_show = strip_tags($markdown_show);
        // exit;
    }

    $title = str_replace([" ", '/', '(', ')'], "", $content['title']);
    file_put_contents($md_dir . "/". $i . ".". $title . ".md", $markdown_show);
    file_put_contents($html_dir . "/".$i . ".". $title . ".html", $content_show);
    $i++;
    // exit;
}

/**
 * 获取章节列表
 * https://api.juejin.cn/booklet_api/v1/booklet/get?aid=xxx&uuid=xxx
 */
function getBooklet(){
    global $host, $api_get_booklet, $aid, $uuid, $booklet_id;
    $uri = $api_get_booklet . "?aid={$aid}&uuid={$uuid}";
    $data = [
        "booklet_id" => $booklet_id,
    ];

    $json = sendPost($host, $uri, $data);
    $arr = json_decode($json, true);
    // print_r($arr);
    return $arr['data']['sections'];
}

/**
 * 获取章节内容
 * https://api.juejin.cn/booklet_api/v1/section/get?aid=xxx&uuid=xxx
 */
function getContent($section_id){
    global $host, $api_get_section, $aid, $uuid, $booklet_id;
    $uri = $api_get_section . "?aid={$aid}&uuid={$uuid}";
    $data = [
        "section_id" => $section_id,
    ];
    $json = sendPost($host, $uri, $data);
    $arr = json_decode($json, true);
    // print_r($arr);
    $section = $arr['data']['section'];

    info("获取到章节信息: section_id:{$section['section_id']}, section_title:{$section['title']}");
    return $section;
}

function sendPost($host ,  $uri, $data){
    global $cookie, $origin;

    // echo $host . $uri ."\n";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host . $uri);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_TIMEOUT,60);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "cookie: ${cookie}",
        "origin: ${origin}",
        "referer: ${origin}",
        "content-type: application/json;charset=UTF-8",
        "user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
    ]);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);
    curl_close ($ch);
    return $server_output;
}

function getConfig()
{
    return parse_ini_file("config.ini");
}